package org.diffshop.product.controller;

import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.R;
import org.diffshop.product.entity.CategoryBrandRelationEntity;
import org.diffshop.product.entity.PmsBrandEntity;
import org.diffshop.product.service.CategoryBrandRelationService;
import org.diffshop.product.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



/**
 * 品牌分类关联
 *
 * @author tjf
 * @date 2024-05-13 10:35:49
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 查询分类关联的品牌
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public R queryBrandByCatId(@RequestParam("catId") Long catId){
        List<PmsBrandEntity> brandEntities = categoryBrandRelationService.queryBrandByCatId(catId);

        List<BrandVO> data = brandEntities.stream().map(brand -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(brand.getBrandId());
            brandVO.setBrandName(brand.getName());
            return brandVO;
        }).collect(Collectors.toList());

        return R.ok().put("data", data);
    }
    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据brandId查询关联分类列表
     * @param brandId
     * @return
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.queryCatelogList(brandId);

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
